package com.heshi.producthomework.controller;

import com.heshi.producthomework.entity.Product;
import com.heshi.producthomework.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductControlller {
    @Autowired
    ProductService productService;

    @RequestMapping("/add")
    public Integer addProduct(@RequestBody Product product){
        return  productService.addProduct(product);
    }

    @GetMapping("/search")
    public List<Product> search(@RequestParam(name = "keyword", required = false) String keyword) {
        return productService.searchByTitle(keyword);
    }

    @GetMapping("/low-stock")
    public List<Product> lowStock() {
        return productService.listLowStockAggregated();
    }

    @GetMapping("/page")
    public List<Product> page(@RequestParam(name = "page", defaultValue = "1") int page,
                              @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return productService.pageList(page, pageSize);
    }
}